import { CHANGE_Order_LIST, CLEAR_ORDER_LIST } from "./mutation-type";
// 数据处理，是添加还是删除
function filter(state, value) {
  if (value.count < 0) return;
  let data = state.order_list;
  for (let i = 0; i < data.length; i++) {
    let _res = data[i];
    // 判断是否已经存在对应的数据
    if (_res.name === value.name) {
      // 当个数为0，移除当前
      if (value.count <= 0) {
        data.splice(i, 1);
        return;
      }
      _res.count = value.count;
      _res.price = _res.count * _res.singlePrice;
      return;
    }
  }
  value.price = value.singlePrice;
  data.push(value);
}

export default {
  [CHANGE_Order_LIST](state, value) {
    filter(state, value);
  },
  [CLEAR_ORDER_LIST](state, value) {
    state.order_list =[]
  },
};
